import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import { generate } from '@feidao/core/querystring';

export default async function a007(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
	console.log('筛选　确定');
	const flag = args[0].target.getAttribute('flag');
	const key = args[0].target.dataset.feidaoId;
	const query = fd.data.params;
	const q = (query.param || (query.param = {}));
	const check_input = fd.data.node.querySelector('[data-feidao-id=check_' + key + ']') as HTMLInputElement;
	let _none = null;
	if (flag == '1') {//日期区间确定
		const begin_input_id = 'begin_' + key;
		const end_input_id = 'end_' + key;
		if (check_input && check_input.checked) {
			q[key] = 0; // 空条件
			window.location.href = generate(query);
		} else {
			const begin = fd.data.node.querySelector("#" + begin_input_id) as HTMLInputElement;
			const end = fd.data.node.querySelector("#" + end_input_id) as HTMLInputElement;
			const b_value = (() => {
				if (begin.type == 'date' && end.type == 'date') {
					const date = new Date(begin.value).getTime();
					return date;
				} else {
					return begin.value;
				}
			})();
			const e_value = (() => {
				if (begin.type == 'date' && end.type == 'date') {
					const date = new Date(end.value).getTime();
					return date;
				} else {
					return end.value;
				}
			})();
			const url_put = (() => {
				if (b_value && e_value) {
					return [b_value, e_value]
				} else if (b_value) {
					return [b_value]
				} else if (e_value) {
					return [null, e_value]
				}
			})();
			if (url_put) {
				q[key] = url_put;
				window.location.href = generate(query);
			}
		}
	} else {//模糊查询确定
		const input_id = 'input_' + key;
		if (check_input.checked) {
			q[key] = ''; // 空条件
			window.location.href = generate(query);
		} else {
			const input = fd.data.node.querySelector('[data-feidao-id=' + input_id + ']') as HTMLInputElement;
			const input_value = input.value;
			if (input_value) {
				q[key] = input_value;
				window.location.href = generate(query);
			}
		}
	}
}
